export default {
	state: {
		// 从 localStorage 获取选中的商品列表，如果没有则初始化为空数组
		selectedGoodsList: JSON.parse(localStorage.getItem("selectedGoodsList") || "[]"),
		sumPrice: JSON.parse(localStorage.getItem("sumPrice") || "[]"), // 用于保存总价
		selectedAllRowLength: JSON.parse(localStorage.getItem("selectedAllRowLength") || "[]"), // 用于保存选中的商品数量
	},

	mutations: {
		// 更新选中的商品列表，并同步到 localStorage
		SET_SELECT_GOODS_LIST(state, goodsList) {
			state.selectedGoodsList = goodsList;
			// 每次修改选中的商品列表时，更新到 localStorage
			localStorage.setItem("selectedGoodsList", JSON.stringify(goodsList));
		},
		// 更新总价
		SET_SUM_PRICE(state, price) {
			state.sumPrice = price;
			localStorage.setItem("sumPrice", JSON.stringify(price));
		},
		// 更新选中的商品数量
		SET_SELECTED_ROW_LENGTH(state, count) {
			state.selectedAllRowLength = count;
			localStorage.setItem("selectedAllRowLength", JSON.stringify(count));
		}
	},

	actions: {
		// 提交选中的商品列表到 store
		updateSelectedGoodsList({
			commit
		}, goodsList) {
			commit('SET_SELECT_GOODS_LIST', goodsList);
		},
		// 提交总价到 store
		updateSumPrice({
			commit
		}, price) {
			commit('SET_SUM_PRICE', price);
		},
		// 提交选中商品数量到 store
		updateSelectedRowLength({
			commit
		}, count) {
			commit('SET_SELECTED_ROW_LENGTH', count);
		}
	},

	getters: {
		// 获取选中的商品列表
		selectedGoodsList: (state) => state.selectedGoodsList,
		sumPrice: (state) => state.sumPrice,
		selectedAllRowLength: (state) => state.selectedAllRowLength,
	}
}